package com.user.service;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.common.util.TablePage;
import com.user.dao.DepartmentRepository;
import com.user.dao.StudentRepository;
import com.user.dao.TeacherRepository;
import com.user.entity.Department;

@Service
@Transactional
public class DepartmentService implements IDepartmentService {
	@Autowired
	private DepartmentRepository departmentDao;
	@Autowired
	private StudentRepository stuDao;
	@Autowired
	private TeacherRepository tchDao;

	public List<Department> findAll() {
		return (List<Department>) departmentDao.findAll();
	}

	public void saveOrUpdate(Department department) {
		departmentDao.save(department);
	}

	public void delete(Long id) {
		departmentDao.delete(id);
	}

	@Override
	public TablePage<Department> findTablePage(Specification<Department> spec, Pageable pageable) {
		Page page = departmentDao.findAll(spec, pageable);
		return TablePage.getTablePage(page);
	}

	@Override
	public boolean hasStudent(Long id) {
		// TODO Auto-generated method stub
		if(stuDao.findByDeptId(id).size()>0)return true;
		return false;
	}
	@Override
	public boolean hasTeacher(Long id) {
		// TODO Auto-generated method stub
		if(tchDao.findByDeptId(id).size()>0)return true;
		return false;
	}

	@Override
	public boolean isNumExist(Department department) {
		// TODO Auto-generated method stub
		Department d = departmentDao.findByNum(department.getNum());
		if(null!=d&&d.getId()!=department.getId())return true;
		return false;
	}

	@Override
	public List<Department> findList() {
		// TODO Auto-generated method stub
		return (List<Department>) departmentDao.findAll();
	}

}
